package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuzhouwei
 * @date 2020/4/16
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 根据当前登录的用户查询当前用户菜单权限
     * @param username
     * @return
     */
    @RequestMapping("/queryMenuListByUsername")
    public Result queryAll(String username) {
        try {
            List<Menu> menuList = menuService.queryMenu(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            if (queryPageBean == null) {
                queryPageBean = new QueryPageBean();
                queryPageBean.setCurrentPage(1);
                queryPageBean.setPageSize(10);
            }
            Integer currentPage = queryPageBean.getCurrentPage();
            Integer pageSize = queryPageBean.getPageSize();
            String queryString = queryPageBean.getQueryString();
            PageResult pageResult = menuService.findPage(currentPage, pageSize, queryString);
            return new Result(true, MessageConstant.QUERY_MENULIST_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENULIST_FAIL);
        }
    }

    @RequestMapping("/findByMenuId")
    public Result findByMenuId(Integer id) {
        try {
            Menu menu = menuService.findByMenuId(id);
            List<Map> parentNameAndId = menuService.findParentNameAndId();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("menu",menu);
            map.put("parentNameAndId",parentNameAndId);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }


    @RequestMapping("/editMenu")
    public Result editMenu(@RequestBody Menu menu) {
        try {
            menuService.editMenu(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }


    @RequestMapping("addMenu")
    public Result addMenu(@RequestBody Menu menu) {
        try {
            menuService.addMenu(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    @RequestMapping("/findParentNameAndId")
    public Result findParentNameAndId(){
        try {
            List<Map> maps = menuService.findParentNameAndId();
            return new Result(true,MessageConstant.QUERY_PARENT_MENUINFO_SUCCESS,maps);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PARENT_MENUINFO_FAIL);
        }
    }
}
